from django.http import HttpResponse, HttpResponseForbidden
import json
import os
from account.models import Account
from trail import settings

result = None


def area_api(request):

    global result
    if not result:
        path = os.path.join(settings.STATICFILES_DATA, 'area.json')
        f = open(path)
        area_list = f.read()
        f.close()
        result = '{"reason": "successed", "result": %s}' % area_list
    return HttpResponse(result)

def remove_avatar(request):

    if 'POST' in request.methods:
        try:
            Account.objects.filter(user=request.user).update(avatar='default/avatar.png')
            result = {'status': 200}
        except:
            result = {'status': 400}
        return HttpResponse(json.dumps(result))
    else:
        return HttpResponseForbidden
